﻿define(['util', 'ajax'], function (util, ajax) {
    "use strict";

    var exports = {},
         ViewModel;

    ViewModel = function (data) {
        var self = this;

        self.id = ko.observable(data.id);
        self.name = ko.observable(data.name);
        self.value = ko.observable(data.value);

        self.cancel = function () {
            window.location = '/template/show?id=' + self.id();
        };

        self.submit = function () {

            var form = {
                Id: self.id(),
                Name: self.name(),
                Value: self.value(),
            };
            ajax.put('template', form).done(function (response) {
                if (response.message != '') {
                    window.location = '/template/show?id=' + self.id();
                }
            })
            ;
        };
    };

    exports.exec = function (sectionId, model) {
        var viewModel = new ViewModel(model);
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };

    return exports;
});